package com.ruoyi.web.controller.task;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.TemplateConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.task.domain.CameraInfo;
import com.ruoyi.task.domain.TaskConfig;
import com.ruoyi.task.domain.TaskDetail;
import com.ruoyi.task.domain.TaskDetailResult;
import com.ruoyi.task.domain.TaskDetailResultParams;
import com.ruoyi.task.domain.ZhTaskImportConfig;
import com.ruoyi.task.service.ICameraService;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/camera/info")
public class CameraController extends BaseController {

    @Autowired
    private ICameraService cameraService;

    /**
     * 查询摄像头管理列表
     */
    @PreAuthorize("@ss.hasPermi('camera:info:list')")
    @GetMapping("/list")
    public TableDataInfo queryCameraList(@RequestParam("deptCode") String deptCode) {
        List<CameraInfo> list = cameraService.queryCameraInfoList(deptCode);
        return getDataTable(list);
    }

    /**
     * 查询摄像头管理列表
     */
    @PreAuthorize("@ss.hasPermi('camera:info:list')")
    @GetMapping("/queryList")
    public TableDataInfo queryList(CameraInfo cameraInfo)
    {
        startPage();
        List<CameraInfo> list = cameraService.selectCameraInfoList(cameraInfo);
        return getDataTable(list);
    }


    /**
     * 获取摄像头相应的图片
     * @param cameraId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('camera:info:picture')")
    @GetMapping("/queryCameraPicture")
    public AjaxResult queryCameraPicture(@RequestParam("cameraId") String cameraId) {
        logger.info("queryCameraPicture param = {}", cameraId);
        return AjaxResult.success("操作成功", cameraService.queryCameraPicture(cameraId));
    }


    /**
     * 新增摄像头管理
     */
    @PreAuthorize("@ss.hasPermi('camera:info:add')")
    @Log(title = "摄像头管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody CameraInfo cameraInfo)
    {
        cameraInfo.setCreateBy(getUsername());
        cameraInfo.setUpdateBy(getUsername());
        return toAjax(cameraService.insertCameraInfo(cameraInfo));
    }


    /**
     * 修改摄像头管理
     */
    @PreAuthorize("@ss.hasPermi('camera:info:edit')")
    @Log(title = "摄像头管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody CameraInfo cameraInfo)
    {
        cameraInfo.setUpdateBy(getUsername());
        return toAjax(cameraService.updateCameraInfo(cameraInfo));
    }


    /**
     * 删除摄像头管理
     */
    @PreAuthorize("@ss.hasPermi('camera:info:remove')")
    @Log(title = "摄像头管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        logger.info("remove param = {}", JSON.toJSONString(ids));
        logger.info("remove username = {}", getUsername());
        return toAjax(cameraService.deleteCameraInfoByIds(ids));
    }

    /**
     * 获取摄像头管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('camera:info:list')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cameraService.selectCameraInfoById(id));
    }


    /**
     * 总行任务参数配置批量导入功能
     * @param file
     * @return
     * @throws IOException
     */
    @PreAuthorize("@ss.hasPermi('camera:info:import')")
    @Log(title = "批量导入", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        ExcelUtil<CameraInfo> excelUtil = new ExcelUtil<>(CameraInfo.class);
        List<CameraInfo> taskConfigs = excelUtil.importExcel(file.getInputStream());
        String username = getUsername();
        return success(cameraService.importCameraConfig(taskConfigs, username));
    }

    /**
     * 批量导入模板下载
     *
     * @param response
     */
    @PreAuthorize("@ss.hasPermi('camera:info:import')")
    @Log(title = "模板导出", businessType = BusinessType.EXPORT)
    @PostMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) {
        try {
            FileUtils.downloadFile(response, "浩云摄像头配置.xlsx",
                    TemplateConstants.CAMERA_TEMPLATE_PATH);
        } catch (Exception e) {
            logger.error("downloadTemplate error ", e);
            throw new RuntimeException("文件导出失败");
        }
    }
}
